<template>
  <el-container class="home_wrapper">
    <!-- 头部结构 -->
    <el-header class="home_header">
      <div class="logo_box">
        <img src="../assets/img/common/heima.png" alt />
        <h4>电商后台管理系统</h4>
      </div>
      <div class="logout">
        <el-button type="info" @click="logout()">退出</el-button>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container class="home_container">
      <!-- 侧边栏 -->
      <el-aside class="home_aside" :width="isCollapse? '64px' : '200px' ">
        <div class="toggle_btn" @click="isCollapse = !isCollapse">|||</div>
        <el-menu
          :router="true"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          v-for="(item,index) in menuLists"
          :key="item.id"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="index+''">
            <!-- 一级菜单的内容 -->
            <template slot="title">
              <i class="iconfont" :class="iconArray[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="(list,i) in item.children"
              :index="list.path"
              :key="i"
              class="el-icon-menu"
            >{{list.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="home_main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      isCollapse: false,
      menuLists: null,
      iconArray: [
        'icon-user',
        'icon-tijikongjian',
        'icon-shangpin',
        'icon-danju',
        'icon-baobiao'
      ]
    }
  },
  components: {},
  created() {
    this.getMenuList()
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.replace('/login')
    },
    async getMenuList() {
      let { data: res } = await this.$http.get('menus')
      if (!res.meta === 200) return res.meta.message
      this.menuLists = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.home_wrapper {
  background-color: #ddd;
  color: #fff;
  height: 100vh;
}
.home_header {
  display: flex;
  justify-content: space-between;
  background-color: #373d41;
  padding: 0;
  .logout {
    padding: 10px;
  }
  .logo_box {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 20px;
    h4 {
      padding-left: 20px;
    }
  }
}
.home_container {
  background-color: #eeeeee;
  .home_aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }
}
.toggle_btn {
  width: 100%;
  color: #fff;
  background-color: #2e97c0;
  text-align: center;
  font-size: 10px;
}
.iconfont {
  margin-right: 10px;
}
.el-icon-menu::before {
  margin-right: 8px;
}
</style>
